package com.wyz.rabbitmqdomo.service;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.UUID;

@Service
public class OrderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init(){

        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ask, String cause) {
                System.out.println("Confirm 被执行了");

                if(ask){
                    System.out.println("消息被正常接收: " + cause);
                }else{
                    System.out.println("消息接收失败: " + cause);
                }

            }
        });

        System.out.println("-------------------------------");

        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage returnedMessage) {
                System.out.println("return 执行了" + returnedMessage.getMessage());
            }
        });

    }

    public void makeOrder(String userid,String productid,int num){

        //1.根据商品id查询库存是否足够
        //2.保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生产成功："+orderId);
        //3.通过MQ来完成消息的分发
        //参数1：交换机 参数2：路由key/queue队列名称 参数3：消息内容
        String exchangeName = "fanout_order_exchange";
        String routingKey = "";

        rabbitTemplate.convertAndSend(exchangeName, routingKey, orderId);

    }

    public void directMakeOrder(String userid,String productid,int num){

        //1.根据商品id查询库存是否足够
        //2.保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生产成功："+orderId);
        //3.通过MQ来完成消息的分发
        //参数1：交换机 参数2：路由key/queue队列名称 参数3：消息内容
        String exchangeName = "direct_order_exchange";
        String routingKey = "";

        rabbitTemplate.convertAndSend(exchangeName, "sms11", "direct" + orderId);
        //rabbitTemplate.convertAndSend(exchangeName, "dx", "direct" + orderId);
    }


    public void topicMakeOrder(String userid,String productid,int num){

        //1.根据商品id查询库存是否足够
        //2.保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生产成功："+orderId);
        //3.通过MQ来完成消息的分发
        //参数1：交换机 参数2：路由key/queue队列名称 参数3：消息内容
        String exchangeName = "topic_order_exchange";
        String routingKey = "com.email.sms.dx";

        rabbitTemplate.convertAndSend(exchangeName, routingKey, "topic" + orderId);
    }

    public void ttlMakeOrder(String userid,String productid,int num){

        //1.根据商品id查询库存是否足够
        //2.保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生产成功："+orderId);
        //3.通过MQ来完成消息的分发
        //参数1：交换机 参数2：路由key/queue队列名称 参数3：消息内容
        String exchangeName = "ttl_direct_exchange";
        String routingKey = "ttl";

        rabbitTemplate.convertAndSend(exchangeName, routingKey, "ttl" + orderId);
    }

    public void ttlMessageMakeOrder(String userid,String productid,int num){

        //1.根据商品id查询库存是否足够
        //2.保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生产成功："+orderId);
        //3.通过MQ来完成消息的分发
        //参数1：交换机 参数2：路由key/queue队列名称 参数3：消息内容
        String exchangeName = "ttl_direct_exchange";
        String routingKey = "message";

        MessagePostProcessor messagePostProcessor =new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {

                message.getMessageProperties().setExpiration("5000");
                message.getMessageProperties().setContentEncoding("UTF-8");
                return message;
            }
        };

        rabbitTemplate.convertAndSend(exchangeName, routingKey, "ttl" + orderId, messagePostProcessor);
    }
}
